import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { BankrollComponent } from "../modal/bankroll/bankroll.component";
import { CheckComponent } from "../modal/check/check.component";
import { CostComponent } from "../modal/cost/cost.component";
import { AnnuityFileComponent } from "../modal/file/file.component";
import { InvestComponent } from "../modal/invest/invest.component";
import { ServiceComponent } from "../modal/service/service.component";
import { InfoComponent } from "../modal/info/info.component";
import { RenewComponent } from "../modal/renew/renew.component";
import { ScaleComponent } from "../modal/scale/scale.component";
import { DataService } from '../../../services/data.service';
import { Util } from '../../../common/util';
import * as _ from 'lodash';

@Component({
  selector: 'annuity-edit',
  templateUrl: './edit.component.html',
  styleUrls: ['./edit.component.scss']
})
export class AnnuityEditComponent implements OnInit {
  // 组合信息模版
  @ViewChild("info", { read: ViewContainerRef })
  info: ViewContainerRef;
  infoComponent: ComponentRef<InfoComponent>;
  // 组合文件模版
  @ViewChild("file", { read: ViewContainerRef })
  file: ViewContainerRef;
  fileComponent: ComponentRef<AnnuityFileComponent>;
  // 费用流水模版
  @ViewChild("cost", { read: ViewContainerRef })
  cost: ViewContainerRef;
  costComponent: ComponentRef<CostComponent>;
  // 资金流水模版
  @ViewChild("bankroll", { read: ViewContainerRef })
  bankroll: ViewContainerRef;
  bankrollComponent: ComponentRef<BankrollComponent>;
  // 核算条目模版
  @ViewChild("check", { read: ViewContainerRef })
  check: ViewContainerRef;
  checkComponent: ComponentRef<CheckComponent>;
  // 服务条目模版
  @ViewChild("service", { read: ViewContainerRef })
  service: ViewContainerRef;
  serviceComponent: ComponentRef<ServiceComponent>;
  // 投资经理模版
  @ViewChild("invest", { read: ViewContainerRef })
  invest: ViewContainerRef;
  investComponent: ComponentRef<InvestComponent>;
  // 续约流水模版
  @ViewChild("renew", { read: ViewContainerRef })
  renew: ViewContainerRef;
  renewComponent: ComponentRef<RenewComponent>;
  // 续约流水模版
  @ViewChild("scale", { read: ViewContainerRef })
  scale: ViewContainerRef;
  scaleComponent: ComponentRef<ScaleComponent>;

  // 路由operate
  public operate: any;
  public index: any;
  // 控制新增tab显示
  public an_isDisplay: any;
  //组合内码
  public nFundNo: any;
  public tabInli = ['zhxx', 'zhwj', 'fyls', 'zjls', 'xyls', 'zcgm', 'hstm', 'fwtm', 'tzjl'];
  public tabIna = ['tab_1', 'tab_2', 'tab_3', 'tab_4', 'tab_8', 'tab_9', 'tab_5', 'tab_6', 'tab_7']

  constructor(
    public resolver: ComponentFactoryResolver,
    private route: ActivatedRoute,
    public dataService: DataService
  ) {
    this.route.params.subscribe((data) => {
      Util.activeFirstTab(this.tabInli, this.tabIna);
      this.operate = data.operate;
      this.nFundNo = data.nFundNo;
      this.index = data.index;
      if (data.index != 'not-running') {
        this.an_isDisplay = 'true'
      } else {
        this.an_isDisplay = 'false'
      }
    });
  }

  ngOnInit() {
    Util.activeFirstTab(this.tabInli, this.tabIna);
    this.showInfo();
  }

  // 组合信息模版
  showInfo() {
    // 判断组件是否已创建，如果创建先注销
    if (this.infoComponent && this.infoComponent.instance) {
      this.infoComponent.destroy();
    }
    // 动态创建组件的实例
    const infoComponent = this.resolver.resolveComponentFactory(InfoComponent);
    this.infoComponent = this.info.createComponent(infoComponent);
  }
  // 组合文件模版
  showFile() {
    // 判断组件是否已创建，如果创建先注销
    if (this.fileComponent && this.fileComponent.instance) {
      this.fileComponent.destroy();
    }
    // 用于控制上传附件框是否显示上传、删除文件按钮
    this.dataService.showUploadBtn = true;
    // 动态创建组件的实例
    const fileComponent = this.resolver.resolveComponentFactory(AnnuityFileComponent);
    this.fileComponent = this.file.createComponent(fileComponent);
  }
  // 费用流水模版
  showCost() {
    if (this.costComponent && this.costComponent.instance) {
      this.costComponent.destroy();
    }
    const costComponent = this.resolver.resolveComponentFactory(CostComponent);
    this.costComponent = this.cost.createComponent(costComponent);
    // this.costComponent.instance.postBody.nFundNo = this.nFundNo;
  }
  // 资金流水模版
  showBankroll() {
    if (this.bankrollComponent && this.bankrollComponent.instance) {
      this.bankrollComponent.destroy();
    }
    const bankrollComponent = this.resolver.resolveComponentFactory(BankrollComponent);
    this.bankrollComponent = this.bankroll.createComponent(bankrollComponent);
    // this.bankrollComponent.instance.getBody.nFundNo = this.nFundNo;
  }
  // 核算条目模版
  showCheck() {
    if (this.checkComponent && this.checkComponent.instance) {
      this.checkComponent.destroy();
    }
    const checkComponent = this.resolver.resolveComponentFactory(CheckComponent);
    this.checkComponent = this.check.createComponent(checkComponent);
  }
  // 服务条目模版
  showService() {
    if (this.serviceComponent && this.serviceComponent.instance) {
      this.serviceComponent.destroy();
    }
    const serviceComponent = this.resolver.resolveComponentFactory(ServiceComponent);
    this.serviceComponent = this.service.createComponent(serviceComponent);
  }
  // 投资经理模版
  showInvest() {
    if (this.investComponent && this.investComponent.instance) {
      this.investComponent.destroy();
    }
    const investComponent = this.resolver.resolveComponentFactory(InvestComponent);
    this.investComponent = this.invest.createComponent(investComponent);
  }
  // 续约流水模版
  showRenew() {
    if (this.renewComponent && this.renewComponent.instance) {
      this.renewComponent.destroy();
    }
    const renewComponent = this.resolver.resolveComponentFactory(RenewComponent);
    this.renewComponent = this.renew.createComponent(renewComponent);
    this.renewComponent.instance.getBody.nFundNo = this.nFundNo
  }
  // 资产规模模版
  showScale() {
    if (this.scaleComponent && this.scaleComponent.instance) {
      this.scaleComponent.destroy();
    }
    const scaleComponent = this.resolver.resolveComponentFactory(ScaleComponent);
    this.scaleComponent = this.scale.createComponent(scaleComponent);
    this.scaleComponent.instance.nFundNo = this.nFundNo;
  }
}

